﻿using System;
using System.Text;
using Deeper.Common;



namespace CodeBuilder
{
	/// <summary>
	/// 基础集合类
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class BaseCollection<T> : System.Collections.CollectionBase where T : class
	{
		public BaseCollection()
		{

		}




		/// <summary>
		/// 索引器
		/// </summary>
		/// <param name="index"></param>
		/// <returns></returns>
		public T this[int index]
		{
			get
			{
				lock (this.List)
				{
					return (T)this.List[index];
				}
			}
		}
 
		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public int Add(T item)
		{
			lock (this.List)
			{
				return this.List.Add(item);
			}
		}

		/// <summary>
		/// 插入
		/// </summary>
		/// <param name="item"></param>
		/// <param name="index"></param>
		public void Insert(T item, int index)
		{
			lock (this.List)
			{

				this.List.Insert(index, item);
			}
		}

		/// <summary>
		/// 删除对象
		/// </summary>
		/// <param name="item"></param>
		public void Remove(T item)
		{
			lock (this.List)
			{
				this.List.Remove(item);
			}
		}

		/// <summary>
		/// 删除索引
		/// </summary>
		/// <param name="index"></param>
		public void Remove(int index)
		{
			lock (this.List)
			{
				this.List.RemoveAt(index);
			}
		}
	}

	[Serializable]
	public sealed class ClassInfo
	{
		/// <summary>
		/// 作者
		/// </summary>
		public string AuthorName { set; private get; }
		private readonly StringBuilder _usingString = new StringBuilder();
		/// <summary>
		/// 引用
		/// </summary>
		public StringBuilder UsingString { get { return _usingString; } }
		/// <summary>
		/// 类前缀
		/// </summary>
		public string Prefix { set; private get; }
		/// <summary>
		/// 类后缀（一般写继承）
		/// </summary>
		public string Postfix { set; get; }
		/// <summary>
		/// 命名空间
		/// </summary>
		public string NameSpace { set; private get; }
		/// <summary>
		/// 名称
		/// </summary>
		public string Name { set; get; }
		private readonly StringBuilder _summary = new StringBuilder();
		/// <summary>
		/// 摘要
		/// </summary>
		public StringBuilder Summary { get { return _summary; } }


		/// <summary>
		/// 模板生成的代码
		/// </summary>
		public string TemplateInfo { set; get; }

		/// <summary>
		/// 类的Attribute
		/// </summary>
		public readonly StringBuilder Attributes = new StringBuilder();

		private readonly StructCollection _structs = new StructCollection();

		/// <summary>
		/// 类的变量
		/// </summary>
		private readonly VariableCollection _variable = new VariableCollection();

		/// <summary>
		/// 属性
		/// </summary>
		public readonly PropertyCollection Properties = new PropertyCollection();

		/// <summary>
		/// 方法
		/// </summary>
		public readonly MethodCollection Methods = new MethodCollection();

		#region 输出tab、注释、参数、花括号
		/// <summary>
		/// 输出tab
		/// </summary>
		/// <param name="iNums">tab的数量</param>
		/// <returns>返回n个tab字符</returns>
		private static string Tabs(int iNums)
		{
			string str = "";
			for (int i = 0; i < iNums; i++)
			{
				str += "\t";
			}
			return str;
		}

		private static string AddSumary(int iTabs, string text)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine(string.Format("{0}/// <summary>", Tabs(iTabs)));
			string[] a = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string b in a)
				sb.AppendLine(string.Format("{0}/// {1}", Tabs(iTabs), b));
			sb.AppendLine(string.Format("{0}/// </summary>", Tabs(iTabs)));
			return sb.ToString();
		}


		private static string AddAttribute(int iTabs, string text)
		{
			StringBuilder sb = new StringBuilder();
			string[] a = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string b in a)
				sb.AppendLine(string.Format("{0}[{1}]", Tabs(iTabs), b));
			return sb.ToString();
		}

		private static string AddParam(int iTabs, string pName, string pText)
		{
			return string.Format("{0}/// <param name=\"{1}\">{2}</param>", Tabs(iTabs), pName, pText);
		}

		private static string AddBrackets(int iTabs, bool isLeft)
		{
			return isLeft ? string.Format("{0}{{", Tabs(iTabs)) : string.Format("{0}}}", Tabs(iTabs));
		}

		#endregion


		private static string OutVariable(int iTabs, Variable var)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(AddSumary(iTabs, var.Summary.ToString()));
			if (var.IsMultLine)
			{
				sb.AppendLine(string.Format("{0}{1} {2} {3} = new {2}", Tabs(iTabs), var.Prefix, var.TypeName, var.Name));
				sb.AppendLine(AddBrackets(iTabs, true));
				string[] contents = var.Content.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string ct in contents)
				{
					sb.AppendLine(string.Format("{0}{1}", Tabs(iTabs + 1), ct));
				}
				sb.AppendLine(AddBrackets(iTabs, false) + ";");

			}
			else
			{
				sb.AppendLine(string.Format("{0}{1} {2} {3} = {4}", Tabs(iTabs), var.Prefix, var.TypeName, var.Name, var.Content));
			}
			return sb.ToString();
		}

		private static string OutStruct(int iTabs, Struct theStruct)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(AddSumary(iTabs, theStruct.Summary.ToString()));
			sb.AppendLine(string.Format("{0}{1} struct {2}", Tabs(iTabs), theStruct.Prefix, theStruct.Name));
			sb.AppendLine(AddBrackets(iTabs, true));
			foreach (Variable var in theStruct.Variables)
			{
				sb.AppendLine(OutVariable(iTabs + 1, var));
			}
			sb.AppendLine(AddBrackets(iTabs, false));
			return sb.ToString();
		}

		public string OutPutClass()
		{
			StringBuilder sb = new StringBuilder();
			foreach (Property pt in Properties)
			{
				sb.Append(AddSumary(2, pt.Summary.ToString()));
				sb.Append(AddAttribute(2, pt.Attributes.ToString()));
				sb.AppendLine(string.Format("{0}{1} {2} {3} {4}", Tabs(2), pt.Prefix, pt.TypeName, pt.Name, pt.Content));
			}
			return sb.ToString();
		}
	}

	#region  方法
	/// <summary>
	/// 参数
	/// </summary>
	public class MethodParams
	{
		public string Name { set; get; }
		public string Summary { set; get; }
		public string Type { set; get; }
		/// <summary>
		/// 是否可为空
		/// </summary>
		public bool IsNullable { set; get; }
	}

	/// <summary>
	/// 方法
	/// </summary>
	public class Method
	{
		public string Prefix { set; get; }
		/// <summary>
		/// 方法名称
		/// </summary>
		public string Name { set; get; }

		private readonly StringBuilder _summary = new StringBuilder();
		/// <summary>
		/// 摘要
		/// </summary>
		public StringBuilder Summary { get { return _summary; } }
		private readonly ParamsCollection _param = new ParamsCollection();
		/// <summary>
		/// 参数
		/// </summary>
		public ParamsCollection Params { get { return _param; } }
		/// <summary>
		/// 返回类型
		/// </summary>
		public string ReturnType { set; get; }
		/// <summary>
		/// 返回摘要
		/// </summary>
		public string ReturnSummary { set; get; }

		private readonly StringBuilder _content = new StringBuilder();
		/// <summary>
		/// 方法内容
		/// </summary>
		public StringBuilder Content { get { return _content; } }

	}
	#endregion
	#region 属性
	/// <summary>
	/// 属性
	/// </summary>
	public class Property
	{
		/// <summary>
		/// 前缀
		/// </summary>
		public string Prefix { set; get; }
		/// <summary>
		/// 数据类型
		/// </summary>
		public string TypeName { set; get; }
		/// <summary>
		/// 名称
		/// </summary>
		public string Name { set; get; }
		private readonly StringBuilder _summary = new StringBuilder();
		/// <summary>
		/// 摘要
		/// </summary>
		public StringBuilder Summary { get { return _summary; } }

		/// <summary>
		/// 类的Attribute
		/// </summary>
		public readonly StringBuilder Attributes = new StringBuilder();


		private readonly StringBuilder _content = new StringBuilder();
		/// <summary>
		/// 内容
		/// </summary>
		public StringBuilder Content { get { return _content; } }
	}
	#endregion

	/// <summary>
	/// 结构体
	/// </summary>
	public abstract class Struct
	{
		/// <summary>
		/// 前缀
		/// </summary>
		public string Prefix { set; get; }
		/// <summary>
		/// 名称
		/// </summary>
		public string Name { set; get; }

		private readonly StringBuilder _summary = new StringBuilder();
		/// <summary>
		/// 注释
		/// </summary>
		public StringBuilder Summary { get { return _summary; } }
		/// <summary>
		/// 结构体成员
		/// </summary>
		public readonly VariableCollection Variables = new VariableCollection();
	}


	/// <summary>
	/// 变量
	/// </summary>
	public abstract class Variable : Property
	{

		public bool IsMultLine { private set; get; }
	}




	#region 属性、参数、方法聚合
	/// <summary>
	/// 属性聚合
	/// </summary>
	public class PropertyCollection : BaseCollection<Property> { }

	/// <summary>
	/// 结构体集合
	/// </summary>
	public class StructCollection : BaseCollection<Struct> { }

	/// <summary>
	/// 变量聚合
	/// </summary>
	public class VariableCollection : BaseCollection<Variable> { }

	/// <summary>
	/// 参数
	/// </summary>
	public class ParamsCollection : BaseCollection<MethodParams> { }

	/// <summary>
	/// 方法聚合
	/// </summary>
	public class MethodCollection : BaseCollection<Method> { }

	#endregion




}

